<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="记录点滴，慢慢成长">
<meta property="og:type" content="website">
<meta property="og:title" content="vicvon&#39;s blog">
<meta property="og:url" content="http://yoursite.com/page/2/index.html">
<meta property="og:site_name" content="vicvon&#39;s blog">
<meta property="og:description" content="记录点滴，慢慢成长">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vicvon&#39;s blog">
<meta name="twitter:description" content="记录点滴，慢慢成长">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/page/2/">





  <title>vicvon's blog</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">vicvon's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br>
            
            站点地图
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/C-and-the-Perils-of-Double-Checked-Locking/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/C-and-the-Perils-of-Double-Checked-Locking/" itemprop="url">C++ and the Perils of Double-Checked Locking</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T17:00:42+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/cplusplus/" itemprop="url" rel="index">
                    <span itemprop="name">cplusplus</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/C-and-the-Perils-of-Double-Checked-Locking/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/C-and-the-Perils-of-Double-Checked-Locking/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p><a href="/resources/C++_and_the_Perils_of_Double-Checked_Locking.pdf">C++ and the Perils of Double-Checked Locking</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/模版参数推导/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/模版参数推导/" itemprop="url">模版参数推导</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T16:51:40+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/cplusplus/" itemprop="url" rel="index">
                    <span itemprop="name">cplusplus</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/模版参数推导/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/模版参数推导/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="模版参数推导"><a href="#模版参数推导" class="headerlink" title="模版参数推导"></a><a href="http://en.cppreference.com/w/cpp/language/template_argument_deduction" target="_blank" rel="noopener">模版参数推导</a></h3><p>类型推导的几个说明：</p>
<ol>
<li>如果模版参数是一个非引用类型<ul>
<li>如果实际参数是一个数组，类型推导会把模版参数推导为指针(数组转指针)</li>
<li>如果实际参数是一个函数，类型推导会把模版参数推导为指针(函数转指针)</li>
<li>如果实际参数是一个cv类型，类型推导会把顶层CV属性忽略</li>
</ul>
</li>
<li>如果模版参数是一个CV类型，类型推导会把顶层CV属性忽略</li>
<li>如果模版参数是一个引用类型，被模版参数引用的类型被用来推导</li>
<li>如果模版参数是一个没有CV限定符的右值引用，并且相应的函数调用参数是一个左值，则使用左值引用类型来推导(这种行为称为<code>forward reference</code>)</li>
</ol>
<p>一般推导过程失败，则考虑以下方案：</p>
<ol>
<li><p>如果模版参数是一个引用类型，被推导类型可以比实际参数多CV限定符</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">template&lt;typename T&gt; void f(const T&amp; t);</span><br><span class="line">bool a = false;</span><br><span class="line">f(a);</span><br><span class="line">/*</span><br><span class="line"> * 模版参数可以被调整为const T(规则3),a实际是bool类型,经过类型推导后a变为const bool类型</span><br><span class="line"> */</span><br></pre></td></tr></table></figure>
</li>
<li><p>传入参数可以是另一个能被<a href="http://en.cppreference.com/w/cpp/language/implicit_conversion#Qualification_conversions" target="_blank" rel="noopener">qualification conversions</a>或函数指针转换的指针或成员指针</p>
</li>
<li>如果模版参数是一个类并且是一个<code>simple-template-id</code>形式，传入参数类型可以是推导类型的派生类，同样的，如果模版参数是一个<code>simple-template-id</code>形式类的指针，传入参数类型可以是推导类型的派生类指针<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">template&lt;class T&gt; struct B &#123; &#125;;</span><br><span class="line">template&lt;class T&gt; struct D : public B&lt;T&gt; &#123; &#125;;</span><br><span class="line">template&lt;class T&gt; void f(B&lt;T&gt;&amp;) &#123; &#125;</span><br><span class="line"> </span><br><span class="line">void f()</span><br><span class="line">&#123;</span><br><span class="line">    D&lt;int&gt; d;</span><br><span class="line">    f(d);</span><br><span class="line">    /*</span><br><span class="line">     * 模版参数是B&lt;T&gt;&amp;,可以被调整为B&lt;T&gt;,传入参数类型是D&lt;int&gt;,T被推导为int,则实际传入类型D&lt;int&gt;是推导类型B&lt;int&gt;的派生类</span><br><span class="line">     */</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/关于top-level-cv-qualifiers/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/关于top-level-cv-qualifiers/" itemprop="url">关于top-level cv-qualifiers</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T16:50:56+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/cplusplus/" itemprop="url" rel="index">
                    <span itemprop="name">cplusplus</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/关于top-level-cv-qualifiers/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/关于top-level-cv-qualifiers/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="top-level-const-和low-level-const"><a href="#top-level-const-和low-level-const" class="headerlink" title="top-level const 和low-level const"></a>top-level const 和low-level const</h3><p>top-level const：const修饰的是自身<br>low-level const：const修饰的是别人</p>
<p>所谓的自身和别人：</p>
<ul>
<li>POD类型，类对象都是自身</li>
<li>指针可以是自身，也可以是别人</li>
<li>引用都是别人</li>
</ul>
<p>通俗记忆<code>const char *</code>是<code>low-level const</code>，而<code>char *const</code>是<code>top-level const</code>。</p>
<p><code>const char *</code>表示指针所指向的内容不可改变，指针本身是可以改变的，相对于<code>low-level const</code>修饰的是别人</p>
<p><code>char * const</code>表示指针本身不可以改变，但是所指向的内容是可以变的，相对于<code>top-level const</code>修饰的自身</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/c-内存模型/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/c-内存模型/" itemprop="url">c++内存模型</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T16:50:13+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/cplusplus/" itemprop="url" rel="index">
                    <span itemprop="name">cplusplus</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/c-内存模型/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/c-内存模型/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="C-11六种内存模型"><a href="#C-11六种内存模型" class="headerlink" title="C++11六种内存模型"></a>C++11六种内存模型</h3><ol>
<li><strong>memory_order_relaxed</strong>: <em>Relaxed operation: there are no synchronization or ordering constraints imposed on other reads or writes, only this operation’s atomicity is guaranteed</em>(松散操作，在别的读/写上没有强制同步或者顺序限制，仅仅保证该操作是原子的)</li>
<li><strong>memory_order_consume</strong>: <em>A load operation with this memory order performs a consume operation on the affected memory location: no reads or writes in the current thread dependent on the value currently loaded can be reordered before this load. Writes to data-dependent variables in other threads that release the same atomic variable are visible in the current thread. On most platforms, this affects compiler optimizations only</em>(使用<code>memory_order_consume</code>的加载操作施加一个<code>consume</code>操作在相关的内存位置<code>memory location</code>: 当前线程，没有依赖当前值的读/写可以重排序在加载之前。在释放相同原子变量的线程中写入数据依赖的变量对当前线程是可见的。在大多数平台，这仅仅影响了编译器的优化)</li>
<li><strong>memory_order_acquire</strong>: <em>A load operation with this memory order performs the acquire operation on the affected memory location: no reads or writes in the current thread can be reordered before this load. All writes in other threads that release the same atomic variable are visible in the current thread</em>(使用<code>memory_order_acquire</code>的加载操作施加一个<code>acquire</code>操作在相关的内存位置：当前线程没有读/写可以被重排序在加载之前。在释放相同原子变量的线程中所有的写对当前线程可见。)</li>
<li><strong>memory_order_release</strong>: <em>A store operation with this memory order performs the release operation: no reads or writes in the current thread can be reordered after this store. All writes in the current thread are visible in other threads that acquire the same atomic variable and writes that carry a dependency into the atomic variable become visible in other threads that consume the same atomic</em>(使用<code>memory_order_release</code>的存储操作施加一个<code>release</code>操作：在当前线程，没有读/写可以重排序在存储之后。当前线程的所有写入对获取相同原子变量的线程可见，并且，写入携带依赖到一个原子变量对消费相同原子变量的线程可见)</li>
<li><strong>memory_order_acq_rel</strong>: <em>A read-modify-write operation with this memory order is both an acquire operation and a release operation. No memory reads or writes in the current thread can be reordered before or after this store. All writes in other threads that release the same atomic variable are visible before the modification and the modification is visible in other threads that acquire the same atomic variable.</em>(<code>memory_order_acq_rel</code>使读改写操作具有<code>acquire</code>和<code>release</code>属性。当前线程，没有读可以重排到存储之前，没有写可以重排到存储之后。释放相同原子变量的线程的所有写入在修改之前是可见的，并且修改对获取相同原子变量的线程是可见的)</li>
<li><strong>memory_order_seq_cst</strong>: <em>Any operation with this memory order is both an acquire operation and a release operation, plus a single total order exists in which all threads observe all modifications in the same order</em>(<code>memory_order_seq_cst</code>的任何操作都是<code>acquire</code>和<code>release</code>的，存在一个顺序，所有的修改对所有的线程有相同的顺序)</li>
</ol>
<h3 id="深入理解C-11中的解释"><a href="#深入理解C-11中的解释" class="headerlink" title="深入理解C++11中的解释"></a>深入理解C++11中的解释</h3><table>
<thead>
<tr>
<th>memory_order</th>
<th>described</th>
</tr>
</thead>
<tbody>
<tr>
<td>memory_order_relaxed</td>
<td>不对执行顺序做任何保证</td>
</tr>
<tr>
<td>memory_order_consume</td>
<td>本线程中，所有后续的有关本原子类型的操作，必须在本条原子操作完成之后执行</td>
</tr>
<tr>
<td>memory_order_acquire</td>
<td>本线程中，所有后续的读操作必须在本条原子操作完成之后执行</td>
</tr>
<tr>
<td>memory_order_release</td>
<td>本线程中，所有之前的写操作完成后才能执行本条原子操作</td>
</tr>
<tr>
<td>memory_order_acq_rel</td>
<td>同时包含<code>memory_order_acquire</code>和<code>memory_order_release</code></td>
</tr>
<tr>
<td>memory_order_seq_cst</td>
<td>全部存取都按顺序执行</td>
</tr>
</tbody>
</table>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/C-标准关于类的解释-POD、trivial、standard-layout/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/C-标准关于类的解释-POD、trivial、standard-layout/" itemprop="url">C++标准关于类的解释(POD、trivial、standard-layout)</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T16:48:52+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/cplusplus/" itemprop="url" rel="index">
                    <span itemprop="name">cplusplus</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/C-标准关于类的解释-POD、trivial、standard-layout/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/C-标准关于类的解释-POD、trivial、standard-layout/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="trivially-copyable类型-平凡拷贝类型，翻译不一定准确-定义："><a href="#trivially-copyable类型-平凡拷贝类型，翻译不一定准确-定义：" class="headerlink" title="trivially copyable类型(平凡拷贝类型，翻译不一定准确)定义："></a>trivially copyable类型(平凡拷贝类型，翻译不一定准确)定义：</h3><ol>
<li>has no not-trivial copy constructors(没有非平凡拷贝构造函数)</li>
<li>has no not-trivial move constructors(没有非平凡移动构造函数)</li>
<li>has no not-trivial copy assignment operators(没有非平凡赋值操作)</li>
<li>has no not-trivial move assignment operators(没有非平凡移动操作)</li>
<li>has a trivial destructor(有一个平凡析构函数)</li>
</ol>
<p>一个trivial类是一个拥有默认构造函数,没有非trivial默认构造函数的trivial copyable类型</p>
<h3 id="standard-layout类型定义："><a href="#standard-layout类型定义：" class="headerlink" title="standard-layout类型定义："></a>standard-layout类型定义：</h3><ol>
<li>has no non-static data members of type non-standard-layout class(or array of such types) or reference(没有非静态成员是非标准布局的类型(或者数组)或者没有引用类型)</li>
<li>has no virtual functions and no virtual base classes(没有虚函数、没有虚基类)</li>
<li>has the same access control for all non-static data members(所有的非静态数据成员有相同的访问权限,要么都是public,要么都是private,要么都是protected)</li>
<li>has no not-standard-layout base classes(没有非标准布局基类)</li>
<li>either has no base classes with non-static data members, or has no non-static data members in the most derived class and at most one base class with non-static data members(没有包含非静态数据成员的基类或者派生类没有非静态数据成员而最多有一个基类有非静态数据成员)</li>
<li>has no base classes of the same type as the first non-static data member(没有与第一个非静态数据成员相同类型的基类)</li>
<li>has no two base class subobject of the same type(没有两个相同类型的基类)</li>
<li>has all non-static data members declared in the same class(either all in the derived or all in some base)(所有的非静态数据成员的声明在同一个类中)</li>
<li></li>
</ol>
<h3 id="POD类型："><a href="#POD类型：" class="headerlink" title="POD类型："></a>POD类型：</h3><p>既是trivial类型也是standard-layout类型</p>
<h3 id="Aggregate"><a href="#Aggregate" class="headerlink" title="Aggregate"></a>Aggregate</h3><p>aggregate类型是一个数据或者是一个没有用户自定义的构造函数、没有私有和保护的非静态数据成员、没有基类、没有虚函数的类</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/利用智能指针的特性设计Time-Wheel-时间轮/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/利用智能指针的特性设计Time-Wheel-时间轮/" itemprop="url">利用智能指针的特性设计Time Wheel(时间轮)</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T16:47:51+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/cplusplus/" itemprop="url" rel="index">
                    <span itemprop="name">cplusplus</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/利用智能指针的特性设计Time-Wheel-时间轮/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/利用智能指针的特性设计Time-Wheel-时间轮/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="利用C-中的智能指针的特性设计Time-Wheel"><a href="#利用C-中的智能指针的特性设计Time-Wheel" class="headerlink" title="利用C++中的智能指针的特性设计Time Wheel"></a>利用C++中的智能指针的特性设计Time Wheel</h3><h5 id="适用场合"><a href="#适用场合" class="headerlink" title="适用场合"></a>适用场合</h5><p>在需要做超时检测的场景中，不需要单独做一个超时检测线程也不用在业务流程中遍历所有待检测对象。</p>
<hr>
<h5 id="时间轮原理"><a href="#时间轮原理" class="headerlink" title="时间轮原理"></a>时间轮原理</h5><p><img src="/resources/time_wheel.jpg" alt="image"></p>
<p>时间轮上每个槽(Bucket)都保存一系列的待检测对象，时间轮在每个检测周期转动一格，当转动到某一格后说明该位置所保存的对象已经到时间，触发这一格所保存对象的超时动作。</p>
<p>利用智能指针的特性可以很容易的实现对象超时后的自动处理工作。每一个Buchet都是一个待检测对象链，链中的每个元素都是一个智能指针对象，管理着需要做超时检测的对象。当时间轮转到某一位置会覆盖当前位置的元素，使原本保存的内容释放，智能指针也会释放内存调用所管理对象的析构函数，此时我们可以在析构函数中做超时处理部分的工作。使用一个入口类保存需要做超时检测的对象，当入口类析构时就可以在入口类的析构函数中调用超时对象的处理操作。</p>
<p>而每当我们需要更新某个对象的时间时，不需要从时间轮中找出该对象，直接将对象对应的入口类保存到时间轮的尾部，只有在智能指针的引用技术为0时才会调用入口类的析构函数，从而简化了超时检测的处理流程。</p>
<h5 id="时间轮的具体实现方法-非唯一"><a href="#时间轮的具体实现方法-非唯一" class="headerlink" title="时间轮的具体实现方法(非唯一)"></a>时间轮的具体实现方法(非唯一)</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">class Entry;</span><br><span class="line">class Object;</span><br><span class="line"></span><br><span class="line">class Object</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">    //...</span><br><span class="line">    void processTimeOut()</span><br><span class="line">    &#123;</span><br><span class="line">        //处理超时</span><br><span class="line">    &#125;</span><br><span class="line">private:</span><br><span class="line">    weak_ptr&lt;Entry&gt; context_;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">class Entry</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">    Entry(const weak_ptr&lt;Object&gt; &amp; obj)</span><br><span class="line">    : weakObj_(obj);</span><br><span class="line">    &#123;</span><br><span class="line">    </span><br><span class="line">    &#125;</span><br><span class="line">    ~Entry()</span><br><span class="line">    &#123;</span><br><span class="line">        shared_ptr&lt;Object&gt; ptr = weakObj_.lock();</span><br><span class="line">        if (ptr)</span><br><span class="line">        &#123;</span><br><span class="line">            ptr-&gt;processTimeOut();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">private:</span><br><span class="line">    shared_ptr&lt;Object&gt; weakObj_;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">typedef shared_ptr&lt;Entry&gt; EntryPtr;</span><br><span class="line">typedef weak_ptr&lt;Entry&gt; WeakEntryPtr;</span><br><span class="line">typedef unordered_set&lt;EntryPtr&gt; Bucket;</span><br><span class="line">typedef boost::circular_buffer&lt;Bucket&gt; TimeWheel;</span><br></pre></td></tr></table></figure>
<p>Entry入口类需要保存Object对象的强引用指针，而Object对象只需保存入口Entry的弱引用指针就可以，避免循环引用导致内存泄漏。</p>
<p>当Object对象被刷新的时候，使用context_构造Entry，加入到TimeWheel中，此时TimeWheel中就保存了多分EntryPtr了。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/关于BOOST无锁队列的用法/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/关于BOOST无锁队列的用法/" itemprop="url">关于BOOST无锁队列的用法</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T16:46:56+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/cplusplus/" itemprop="url" rel="index">
                    <span itemprop="name">cplusplus</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/关于BOOST无锁队列的用法/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/关于BOOST无锁队列的用法/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="关于BOOST的无锁队列"><a href="#关于BOOST的无锁队列" class="headerlink" title="关于BOOST的无锁队列"></a>关于BOOST的无锁队列</h3><p>queue有几个关键的模版参数：</p>
<ol>
<li><code>boost::lockfree::fixed_sized</code> 如果设置为true，则当push元素到队列容量时就会返回false，即队列满不在添加元素到队列；如果设置为false，则当push元素到队列容量时会调用系统的api分配空间存储期望加入队列的元素，这就不一定还是lockfree的了。</li>
<li><code>boost::lockfree::capacity</code> 指定队列的大小，并且默认会开启<code>boost::lockfree::fixed_sized</code>为true，即队列满在无法添加元素会返回false。</li>
</ol>
<p>queue的几种定义固定容量的无锁队列方式：</p>
<ol>
<li><code>boost::lockfree::queue&lt;int, boost::lockfree::capacity&lt;128&gt; &gt; q</code></li>
<li><code>boost::lockfree::queue&lt;int, boost::lockfree::fixed_sized&lt;true&gt; &gt; q(128)</code></li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/单例模式double-check存在的问题/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/单例模式double-check存在的问题/" itemprop="url">单例模式double-check存在的问题</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T16:45:44+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/cplusplus/" itemprop="url" rel="index">
                    <span itemprop="name">cplusplus</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/单例模式double-check存在的问题/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/单例模式double-check存在的问题/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="单例模式双检索方式在多线程程序中存在的问题"><a href="#单例模式双检索方式在多线程程序中存在的问题" class="headerlink" title="单例模式双检索方式在多线程程序中存在的问题"></a>单例模式双检索方式在多线程程序中存在的问题</h3><p>先看单例模式双检索方式代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">class Singleton()</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">    //...</span><br><span class="line">    Singleton * getInstance()</span><br><span class="line">    &#123;</span><br><span class="line">        if (instance == nullptr)</span><br><span class="line">        &#123;</span><br><span class="line">            lock(); // 加锁</span><br><span class="line">            if ( instance == nullptr)</span><br><span class="line">            &#123;</span><br><span class="line">                instance = new Singleton();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        return instance;</span><br><span class="line">    &#125;</span><br><span class="line">private:</span><br><span class="line">    Singleton *instance;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>双检索的目的是为了防止在第一次检查时对象还没有被创建，在等待锁的过程中其他地方创建了对象，所以在获得锁后再次检查对象是否被创建，否则可能会创建2个对象。</p>
<p>但是双检索的方式在多线程的环境下同样会出现问题，因为对象的创建不是原子操作。对象的创建要经历3个过程：</p>
<ol>
<li>创建一段可以容纳对象的内存空间；</li>
<li>调用对象的构造函数；</li>
<li>将该段内存地址赋值给指针变量；<br>而以上3步并不能保证是按1-2-3的顺序完成的，有可能是1-3-2的顺序完成，如果先把内存地址赋值给了指针变量但是对象还没有构造好，另一个线程在使用的过程中检查会不成立直接返回对象地址导致错误发生。</li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/自动获取tuple中某种类型的元素/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/自动获取tuple中某种类型的元素/" itemprop="url">自动获取tuple中某种类型的元素</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T16:44:12+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/cplusplus/" itemprop="url" rel="index">
                    <span itemprop="name">cplusplus</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/自动获取tuple中某种类型的元素/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/自动获取tuple中某种类型的元素/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="自动获得tuple中某种类型的元素代码学习分析"><a href="#自动获得tuple中某种类型的元素代码学习分析" class="headerlink" title="自动获得tuple中某种类型的元素代码学习分析"></a>自动获得tuple中某种类型的元素代码学习分析</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;tuple&gt;</span><br><span class="line"></span><br><span class="line">using namespace std;</span><br><span class="line"></span><br><span class="line">template &lt;class T, std::size_t N, class... Args&gt;</span><br><span class="line">struct indexOf;</span><br><span class="line"></span><br><span class="line">template &lt;class T, std::size_t N, class... Args&gt;</span><br><span class="line">struct indexOf&lt;T, N, T, Args...&gt; : std::integral_constant&lt;int, N&gt;</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">template &lt;class T, std::size_t N, class U, class... Args&gt;</span><br><span class="line">struct indexOf&lt;T, N, U, Args...&gt; : std::integral_constant&lt;int, indexOf&lt;T, N + 1, Args...&gt;::value&gt;</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">template &lt;class T, std::size_t N&gt;</span><br><span class="line">struct indexOf&lt;T, N&gt; : std::integral_constant &lt; int, -1 &gt;</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">template &lt;class T, class... Args&gt;</span><br><span class="line">T get_element_by_type(const std::tuple&lt;Args...&gt;&amp; t)</span><br><span class="line">&#123;</span><br><span class="line">    return std::get&lt;indexOf&lt;T, 0, Args...&gt;::value&gt;(t);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>测试用例<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">tuple&lt;int, double, char, short&gt; tp = make_tuple(1, 2.3, 2, 1);</span><br><span class="line">auto r = get_element_by_type&lt;double&gt;(tp);  //r = 2.3</span><br></pre></td></tr></table></figure></p>
<p>模版实例化过程分析：</p>
<ol>
<li><p>当调用<code>get_element_by_type&lt;double&gt;(tp)</code>，模版被实例化为<code>double get_element_by_type(const tuple&lt;int, double, char, short&gt;&amp; tp)</code>,然后调用<code>get&lt;indexOf&lt;double, 0, int, double, char, short&gt;::value&gt;(t)</code></p>
</li>
<li><p>实例化<code>indexOf</code>类，因为模版参数第一个和第三个不是一种类型，所以特化第二个模版，实例化为<code>indexOf&lt;double, 0 + 1, double, char, short&gt;</code></p>
</li>
<li><p>继续实例化模版类，因为第一个和第三个模版参数类型一样，所以实例化第一个模版，从而返回<code>indexOf::value</code>的值1</p>
</li>
<li><p>最终实际调用的是<code>get&lt;1&gt;(tp)</code>，即返回<code>tuple</code>中第二个元素</p>
</li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/09/TCP客户端-服务端各种异常连接分析/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vicvon">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vicvon's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/10/09/TCP客户端-服务端各种异常连接分析/" itemprop="url">TCP客户端/服务端各种异常连接分析</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-09T09:57:38+08:00">
                2017-10-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/network/" itemprop="url" rel="index">
                    <span itemprop="name">network</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/09/TCP客户端-服务端各种异常连接分析/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/10/09/TCP客户端-服务端各种异常连接分析/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="TCP客户端和服务端各种异常连接分析-UNP第5章"><a href="#TCP客户端和服务端各种异常连接分析-UNP第5章" class="headerlink" title="TCP客户端和服务端各种异常连接分析(UNP第5章)"></a>TCP客户端和服务端各种异常连接分析(UNP第5章)</h3><ol>
<li><p>accept返回前连接终止<br> <div id="sequence-0"></div><br> 三次握手完成，调用accept前有耗时操作，此时客户端发送RST到服务端，根据不同的实现accept会有不同的返回值。</p>
</li>
<li><p>服务器进程终止</p>
<p> 服务器进程终止会关闭所有文件描述符，发送FIN到客户端，客户端收到FIN后会发送ACK，这时四次挥手已经完成一半。等待客户端关闭文件描述符，发送FIN到服务端。</p>
</li>
<li><p>服务器主机崩溃</p>
<p> 服务器主机崩溃后，再次发送数据会收到ETIMEOUT或EHOSTUNREACH错误，如果想快速检测出这种错误需要设置一个超时，或者设置SO_KEEPALIVE套接字选项。</p>
</li>
<li><p>服务器崩溃后重启</p>
<p> 当服务器崩溃重启后，它的所有连接信息都丢失，因此服务器对于所有收到的来自客户机的数据都响应RST。</p>
</li>
<li><p>服务器主机关机</p>
<p> 和服务器进程终止情况类似。<br><script src="https://cdnjs.cloudflare.com/ajax/libs/webfont/1.6.27/webfontloader.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/snap.svg/0.4.1/snap.svg-min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/js-sequence-diagrams/1.0.6/sequence-diagram-min.js"></script><textarea id="sequence-0-code" style="display: none">    Note left of client: connect阻塞
    client->server: SYN
    Note right of server: SYN_RCVD
    server->client: SYN+ACK
    Note left of client: connect 返回
    client->server: ACK
    Note right of server: ESTABLISHED
    client->server: RST
    Note right of server: 调用accept</textarea><textarea id="sequence-0-options" style="display: none">{"scale":1,"line-width":2,"line-length":50,"text-margin":10,"font-size":12,"theme":"simple"}</textarea><script>  var code = document.getElementById("sequence-0-code").value;  var options = JSON.parse(decodeURIComponent(document.getElementById("sequence-0-options").value));  var diagram = Diagram.parse(code);  diagram.drawSVG("sequence-0", options);</script></p>
</li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="page-number current">2</span><a class="page-number" href="/page/3/">3</a><a class="extend next" rel="next" href="/page/3/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/uploads/avatar.jpg" alt="vicvon">
            
              <p class="site-author-name" itemprop="name">vicvon</p>
              <p class="site-description motion-element" itemprop="description">记录点滴，慢慢成长</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">27</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/vicvon" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">true</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        




  <script type="text/javascript">
    (function() {
      var hm = document.createElement("script");
      hm.src = "//tajs.qq.com/stats?sId=63333898";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  

    
      <script id="dsq-count-scr" src="https://vicvon1.disqus.com/count.js" async></script>
    

    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
